package com.rzgeo.rzmaplib.comp;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.rzgeo.rzmaplib.R;


/**
 * Created by zhanghang on 2017/10/27.
 */
public class RmlLayerListItem extends RelativeLayout {

//    private Spinner _spMode;
    private Switch _swShow;
    private RmlLayerList.LayerHolder _layerHolder;
    private CompoundButton.OnCheckedChangeListener _onItemClick=new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(_layerHolder.getLayerType()== RmlLayerList.LayerType.graphicOverLayer)
                _layerHolder.graphicsOverlay.setVisible(isChecked);
            else
                _layerHolder.layer.setVisible(isChecked);
        }
    };

    private TextView _txv_title;
    private ImageButton _btnConf;
    private RmlLayerList _layerList;

    public RmlLayerListItem(Context context) {
        super(context);
    }

    public RmlLayerListItem(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    public RmlLayerListItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setup(RmlLayerList.LayerHolder layerHolder, RmlLayerList layerList) {
        _layerHolder = layerHolder;
        _layerList=layerList;
        if (
                _swShow != null&&
                _txv_title!=null)
            updateFromLayer();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        _txv_title= findViewById(R.id.txv_title);
        _swShow = findViewById(R.id.sw_show);

        if (_layerHolder != null)
            updateFromLayer();
    }

    private void updateFromLayer() {
        _txv_title.setText(_layerHolder.Title);

        _swShow.setOnCheckedChangeListener(_onItemClick);
        if(_layerHolder.getLayerType()== RmlLayerList.LayerType.graphicOverLayer)
            _swShow.setChecked(_layerHolder.graphicsOverlay.isVisible());
        else
            _swShow.setChecked(_layerHolder.layer.isVisible());
    }

}
